/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 39891
 * Date: 2025-04-10
 * Time: 16:23
 */
import java.util.*;
public class Test {
    public static void calcWordsNum(String[] arr){
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0; i < arr.length ; i++){
            int ans = 0;
            if(!map.containsKey(arr[i])){
                map.put(arr[i],ans+1);
            }else{
                //如果包含的话 就更新val
                int oldVal = map.get(arr[i]);
                map.put(arr[i],oldVal + 1);
            }
        }
        //放着每个节点的集合
        Set<Map.Entry<String,Integer>> set  =map.entrySet();
        for (Map.Entry<String,Integer> entry:set) {
            System.out.println(entry.getKey() +"出现的次数为："
                    +entry.getValue());

        }
        //System.out.println(map);
    }
    //计算出现的单词次数
    public static void main(String[] args) {
        String[] words = {"apple", "you","rei","rwe","you","rei","das","fwq","you","rei",};
        calcWordsNum(words);
    }
    //去重所有数据
    public static void main2(String[] args) {
        int[] arr = {1,2,3,4,5,6,2,45,5};
        Set<Integer> set = new HashSet<>();
        for(int i = 0; i < arr.length ; i++){
            set.add(arr[i]);
        }
        System.out.println(set);
    }

    public static void main1(String[] args) {
        //求第一个重复的数据 有重复的就结束循环 打印重复的
        int[] arr = {1,2,3,4,5,6,2,45,5};

        Set<Integer> set = new HashSet<>();
        for(int i = 0; i < arr.length ; i++){
            if(!set.contains(arr[i])){
                set.add(arr[i]);
            }else {
                System.out.println(arr[i]);
                break;
            }
        }
    }
}
